package com.mj.mes.system.SysMechanics;

import com.mj.factory.kanBan.KanBanController;
import org.apache.commons.collections.map.HashedMap;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/8/21.
 */
@Controller
@RequestMapping("mechanics")
public class SysMechanicsController {
    private Logger logger = Logger.getLogger(this.getClass());

    @Autowired
    private SysMechanicsService sysMechanicsService;
    @Autowired
    KanBanController kanBanController;

    /**
     * 分页查询
     */
    @PostMapping(value = "queryWithPaging")
    @ResponseBody
    public Map<String, Object> findAll(@RequestParam(value = "fieldName", required = false, defaultValue = "") String deviceName,
                                       SysMechanics sysMechanics,
                                       Integer page, Integer rows,
                                       @RequestParam(value = "rows") Integer size) {
        Map<String, Object> map = new HashedMap();
        int total;
        Page<SysMechanics> list;
        if (deviceName.equals("")) {

            list = sysMechanicsService.findAll(new PageRequest(page - 1, size));
            total = sysMechanicsService.findAll().size();
        } else {
            list = sysMechanicsService.findByDeviceName(deviceName, new PageRequest(page - 1, size));
            total = sysMechanicsService.findByDeviceName(deviceName, new PageRequest(page - 1, size)).getSize();
        }
        map.put("total", total);
        map.put("rows", list.getContent());
        return map;
    }

    /**
     * 添加设备
     *
     * @param
     * @return
     */
    @RequestMapping(value = "addMechanics", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> addMechanics(SysMechanics sysMechanics) {
        Map<String, Object> map = new HashedMap();
        //默认状态为1(空闲)
        try {
            sysMechanicsService.addMechanics(sysMechanics);
            map.put("msg", "success");
        } catch (Exception e) {
            map.put("msg", "fail");
            logger.error(e);
        }
        return map;
    }

    /**
     * 修改设备
     *
     * @param
     * @return
     */
    @RequestMapping(value = "update", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> update(SysMechanics sysMechanics) {
        Map<String, Object> map = new HashedMap();
        //默认状态为1(空闲)
        try {
            sysMechanicsService.updateMechanics(sysMechanics);
            map.put("msg", "success");
        } catch (Exception e) {
            map.put("msg", "fail");
            logger.error(e);
        }
        return map;
    }

    /**
     * 根据ID返回实体
     */
    @RequestMapping(value = "toUpdate", method = RequestMethod.GET)
    public String toUpdateMechanics(@RequestParam("id") Long id, Model model) {
        SysMechanics sysMechanics = sysMechanicsService.findOne(id);
        model.addAttribute("id", sysMechanics.getId());
        model.addAttribute("deviceName", sysMechanics.getDeviceName());
        model.addAttribute("deviceState", sysMechanics.getDeviceState());
        model.addAttribute("devicePosition", sysMechanics.getDevicePosition());
        return "mechanics/MechanicsEdit";
    }

    /**
     * 删除设备
     *
     * @param ids
     * @return
     */
    @RequestMapping(value = "delete", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, String> deletMechanics(@RequestParam("ids") String ids) {
        Map<String, String> map = new HashedMap();
        String[] idArrayList = ids.split(",");
        List<SysMechanics> list = new ArrayList<SysMechanics>();
        for (int i = 0; i < idArrayList.length; i++) {
            SysMechanics sysMechanics = new SysMechanics();
            sysMechanics.setId(Long.valueOf(idArrayList[i]));
            list.add(sysMechanics);
        }
        boolean flag = sysMechanicsService.delete(list);
        if (flag) {
            map.put("msg", "success");
        } else {
            map.put("msg", "fail");
        }
        return map;
    }


    /**
     * 分页查询
     */
    @PostMapping(value = "queryAllColor")
    @ResponseBody
    public Map<String, Object> findAllColor() {
        Map<String, Object> map = new HashedMap();
        int length;
        List<SysMechanics> list;
        list = sysMechanicsService.findAll();
        length = sysMechanicsService.findAll().size();

        map.put("length", length);
        map.put("items", list);
        return map;
    }

//    @RabbitListener(queues = "DEVICE_SIMULATOR_QUEUE", containerFactory="rabbitListenerContainerFactory")
//    public void process(Messages<MachineStatusChange> messages) throws IOException, ClassNotFoundException {
////        ObjectMapper objectMapper = new ObjectMapper();
////        String json = new ObjectMapper().writeValueAsString(object);
////        logger.info(json);
//
//        String handlerClassName = messages.getMessageName();
//        switch (handlerClassName){
//            case "MachineStatusChange":
//                Class c = Class.forName("MachineStatusChange");
//
//                MachineStatusChange msc = (MachineStatusChange) messages.getBody();
//                SysMechanics sysMechanics = sysMechanicsService.findByDevicePosition(msc.getMachineName());
//                sysMechanics.setDeviceState(Integer.parseInt(msc.getCurrentStatus()));
//                sysMechanicsService.updateMechanics(sysMechanics);
//                KanBanDevice kanBanDevice = new KanBanDevice();
//                kanBanDevice.setName(sysMechanics.getDevicePosition());
//                kanBanDevice.setColor(String.valueOf(sysMechanics.getDeviceState()));
//                kanBanController.changeKanBanDeviceColor(kanBanDevice);
//                break;
//            default:
//                return;
//        }
//    }
}
